﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RenWuZhi.Models;
using System.Web.Security;

namespace RenWuZhi.Controllers
{
    public class PeopleContentController : Controller
    {
        //
        // GET: /PeopleContent/
        Models.RenWuZhiDBEntities _db;

        public ActionResult Index(string id)
        {

            Models.AccountMembershipService models = new AccountMembershipService();          

            if (id == null)
            {
                MembershipUser entityUser = models.GetByUserName(HttpContext.User.Identity.Name);
                Guid guserid = new Guid(entityUser.ProviderUserKey.ToString());
                ViewData.Model = from entityList in _db.rwz_PeopleContent.ToList()
                                 orderby entityList.PC_UpdateDate descending, entityList.PC_CreateDate descending
                                 where entityList.UserID == guserid
                                 select entityList;
            }
            else
            {
                ViewData.Model = from entityList in _db.rwz_PeopleContent.ToList()
                                 orderby entityList.PC_UpdateDate descending, entityList.PC_CreateDate descending
                                 where entityList.PC_Content.IndexOf(id) > 0 || entityList.PC_Title.IndexOf(id) > 0
                                 select entityList;
            }
            return View();
        }

        public PeopleContentController()
        {
            _db = new Models.RenWuZhiDBEntities();
        }
        //
        // GET: /PeopleContent/Details/5

        public ActionResult Details(string id)
        {
            var entity = _db.rwz_PeopleContent.First(m => m.PC_ID == new Guid(id));

            ViewData.Model = entity;
            return View(entity);
        }

        //
        // GET: /PeopleContent/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /PeopleContent/Create
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                var entity = new rwz_PeopleContent();
                entity.PC_ID = Guid.NewGuid();
                Models.AccountMembershipService models = new AccountMembershipService();
                MembershipUser entityUser = models.GetByUserName(HttpContext.User.Identity.Name);
                entity.UserID = new Guid(entityUser.ProviderUserKey.ToString());
                entity.PC_CreateDate = DateTime.Now;
                entity.PC_UpdateDate = DateTime.Now;
                entity.PC_Flag = 1;
                TryUpdateModel(entity, new string[] { "PC_Title", "PC_Content", "PC_Comment" }, collection.ToValueProvider());
                //entity.PC_Content = Server.HtmlEncode(entity.PC_Content);
                if (String.IsNullOrEmpty(entity.PC_Title))
                    ModelState.AddModelError("PC_Title", "标题不能为空！");
                if (String.IsNullOrEmpty(entity.PC_Content))
                    ModelState.AddModelError("PC_Content", "内容不能为空！");
                if (ModelState.IsValid)
                {
                    _db.AddTorwz_PeopleContent(entity);
                    _db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(entity);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PeopleContent/Edit/5
        public ActionResult Edit(string id)
        {
            var entity = _db.rwz_PeopleContent.First(m => m.PC_ID == new Guid(id));

            ViewData.Model = entity;
            return View(entity);
        }

        //
        // POST: /PeopleContent/Edit/5
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                var id = collection["PC_ID"];
                var entity = _db.rwz_PeopleContent.First(m => m.PC_ID == new Guid(id));
                entity.PC_UpdateDate = DateTime.Now;
                TryUpdateModel(entity, new string[] { "PC_Title", "PC_Content", "PC_Comment" }, collection.ToValueProvider());
                if (String.IsNullOrEmpty(entity.PC_Title))
                    ModelState.AddModelError("PC_Title", "标题不能为空！");
                if (String.IsNullOrEmpty(entity.PC_Content))
                    ModelState.AddModelError("PC_Content", "内容不能为空！");

                if (ModelState.IsValid)
                {
                    _db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(entity);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PeopleContent/Delete/5

        public ActionResult Delete(string id)
        {
            var entity = _db.rwz_PeopleContent.First(m => m.PC_ID == new Guid(id));

            _db.DeleteObject(entity);
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        //
        // POST: /PeopleContent/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
